var jam = null;
addZero = function(val)
{
    if (val >= 1 && val <= 9) {
        return "0" + val;
    } else {
        return val;
    }
};

/*** Date Function ***********/
Date.prototype.incMinutes = function(m) {
    var copiedDate = new Date(this.getTime());
    copiedDate.setMinutes(copiedDate.getMinutes() + m);
    return copiedDate;
};
Date.prototype.decMinutes = function(m) {
    var copiedDate = new Date(this.getTime());
    copiedDate.setMinutes(copiedDate.getMinutes() - m);
    return copiedDate;
};
Date.prototype.incHours = function(h) {
    var copiedDate = new Date(this.getTime());
    copiedDate.setHours(copiedDate.getHours() + h);
    return copiedDate;
};
Date.prototype.decHours = function(h) {
    var copiedDate = new Date(this.getTime());
    copiedDate.setHours(copiedDate.getHours() - h);
    return copiedDate;
};

dateTimeToHRA = function(dt) {
    var datetime = dt.getFullYear() + "-"
            + addZero(dt.getMonth() + 1) + "-"
            + addZero(dt.getDate()) + " "
            + addZero(dt.getHours()) + ":"
            + addZero(dt.getMinutes()) + ":"
            + addZero(dt.getSeconds());
    return datetime;
};
currentDate = function() {
    var dt = new Date();
    return dateTimeToHRA(dt);
};
incHourToCurrentDate = function(hour) {
    var dt = new Date();
    return(dateTimeToHRA(dt.incHours(hour)));
};
decHourToCurrentDate = function(hour) {
    var dt = new Date();
    return(dateTimeToHRA(dt.decHours(hour)));
};
decMinuteToCurrentDate = function(minute) {
    var dt = new Date();
    return(dateTimeToHRA(dt.decMinutes(minute)));
};
incMinuteToCurrentDate = function(minute) {
    var dt = new Date();
    return(dateTimeToHRA(dt.incMinutes(minute)));
};

indDate = function(d)
{
    var fr = d.split(' ');
    var er = fr[0].split('-');
    var id = er[2] + " " + dMonth(er[1]) + " " + er[0] + " " + fr[1];
    return id;
};
parseDate = function(d)
{
    var p = d.split(' ');
    var q = p[0].split('-');
    var r = p[1].split(':');
    return new Date(q[0], q[1] - 1, q[2], r[0], r[1], r[2]);
};
countDays = function(d1, d2)
{
    var a_day = 1000 * 60 * 60 * 24;
    var d1_date = parseDate(d1);
    var d2_date = parseDate(d2);
    var d1_ms = d1_date.getTime();
    var d2_ms = d2_date.getTime();
    var diff_ms = d2_ms - d1_ms;
    return Math.round(diff_ms / a_day);
};


dMonth = function(m)
{
    var b;
    switch (String(m)) {
        case '01':
            b = 'Januari';
            break;
        case '02':
            b = 'Pebruari';
            break;
        case '03':
            b = 'Maret';
            break;
        case '04':
            b = 'April';
            break;
        case '05':
            b = 'Mei';
            break;
        case '06':
            b = 'Juni';
            break;
        case '07':
            b = 'Juli';
            break;
        case '08':
            b = 'Agustus';
            break;
        case '09':
            b = 'Spetember';
            break;
        case '10':
            b = 'Oktober';
            break;
        case '11':
            b = 'Nopember';
            break;
        case '12':
            b = 'Desember';
            break;
        default:
            break;
    }
    return b;
};


formatNumber = function(num) {
    var array = num.toString().split('');
    var index = -3;
    while (array.length + index > 0) {
        array.splice(index, 0, ',');
        index -= 4;
    }
    return array.join('');
};
dName = function(a) {
    if ((a >= 337.5) && (a >= 0) && (a <= 22))
        return "Utara";
    if ((a >= 22.5) && (a <= 67))
        return "Timur Laut";
    if ((a >= 67.5) && (a <= 112))
        return "Timur";
    if ((a >= 112.5) && (a <= 157))
        return "Tenggara";
    if ((a >= 157.5) && (a <= 202))
        return "Selatan";
    if ((a >= 202.5) && (a <= 247))
        return "Barat Daya";
    if ((a >= 247.5) && (a <= 292))
        return "Barat";
    if ((a >= 292.5) && (a <= 337))
        return "Barat Laut";
};
eState = function(s) {
    if (s === 0)
        return "<div class='red'>OFF</div>";
    else
        return "<div class='green'>ON</div>";
};
addZero = function(val)
{
    var tmp = val;
    if (val <= 9)
        tmp = "0" + val;
    return tmp;
};


function fspeed(num, row) {
    return num + " Km/Jam";
}
function fodo(num, row) {
    var val = parseFloat(num);
    if (val >= 1000) {
        //alert(val);
        return (val / 1000).toFixed(2) + " Km";
    } else {
        return val + " Meter";
    }
}
function acc_state_format(value, row, index) {
    if (value === '0' || value === 0) {
        return 'background-color:#ffee00;color:red;';
    } else {
        return 'background-color:green;color:red;';
    }
}
function logout() {
    if (confirm("Logout ?"))
        window.location = "php_script/do_logout.php";
}
function distance(lat1, lng1, lat2, lng2) {
    var R = 6371; // Radius of the earth in km
    var dLat = deg2rad(lat2 - lat1);  // deg2rad below
    var dLon = deg2rad(lng2 - lng1);
    var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
            Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *
            Math.sin(dLon / 2) * Math.sin(dLon / 2);
    var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    var d = R * c; // Distance in km
    return d;
}

function deg2rad(deg) {
    return deg * (Math.PI / 180);
}
/*
 * Diff Two DateTime, and return Human Read able
 * @returns {undefined}
 */
diff_datetime_to_hra = function(start, finish) {
    var diff = new Date(start) - new Date(finish);
    var strHumanReadable = "";
    if (diff > 0) {
        //tobe minute
        diff = diff / 1000 / 60;

        //Get Days
        if (diff >= (60 * 24)) {
            diff = (diff / (60 * 24));
            strHumanReadable = parseInt(diff, 10) + " Hari ";
        }
        //Get Hour
        if (diff >= 60) {
            diff = diff / 60;
            strHumanReadable = strHumanReadable + " " + parseInt(diff, 10) + " Jam ";
        }

        //Get Minute
        if (diff >= 1) {
            strHumanReadable = strHumanReadable + " " + parseInt(diff, 10) + " Menit ";
        }
    }
    return strHumanReadable;
};
diff_minute = function(current, prev) {
    var diff = current - prev;
    if (diff > 0) {
        diff = diff / 1000 / 60;
    }
    return diff;
};

diff_datetime = function(d1, d2) {
    var arr = d1.split(" ");
    var arrDate = arr[0].split("-");
    var arrTime = arr[1].split(":");
    var arr2 = d2.split(" ");
    var arrDate2 = arr2[0].split("-");
    var arrTime2 = arr2[1].split(":");
    var t1 = new Date(arrDate[0], arrDate[1], arrDate[2], arrTime[0], arrTime[1], arrTime[2]);
    var t2 = new Date(arrDate2[0], arrDate2[1], arrDate2[2], arrTime2[0], arrTime2[1], arrTime2[2]);
    var ms=parseInt(t2-t1)/1000/60;
    return parseInt(ms);
};
/*
 * Convert Date Format ddd/mm/yyyy to yyyy-mm-dd
 */
to_mysql_date = function(str) {
    var arr = str.split("/");
    return arr[2] + "-" + arr[1] + "-" + arr[0];
};

//diff_datetime = function(d1, d2) {
//    var arr = d1.split(" ");
//    var arrDate = arr[0].split("-");
//    var arrTime = arr[1].split(":");
//    var arr2 = d2.split(" ");
//    var arrDate2 = arr2[0].split("-");
//    var arrTime2 = arr2[1].split(":");
//    var t1 = new Date(parseInt(arrDate[0]), parseInt(arrDate[1]), parseInt(arrDate[2]), parseInt(arrTime[0]), parseInt(arrTime[1]), parseInt(arrTime[2]));
//    var t2 = new Date(parseInt(arrDate2[0]), parseInt(arrDate2[1]), parseInt(arrDate2[2]), parseInt(arrTime2[0]), parseInt(arrTime2[1]), parseInt(arrTime2[2]));
//    var ms=parseInt(t2-t1)/1000/60;
//    return parseInt(ms);
//};
convert_acc_state = function(state) {
    var str = "";
    switch (state) {
        case 1:
            str = "ON";
            break;
        case 0:
            str = "OFF";
            break;
        default:
            str = "NA";
            break;
    }
    return str;
};
convert_batt_level = function(level) {
    var str = "";
    switch (level) {
        case 0:
            str = "Empty";
            break;
        case 1:
            str = "Warning";
            break;
        default:
            str = "N/A";
            break;
    }
    return str;
};

get_acc_state = function(state) {
    console.log('ACC STATE:' + state);
    if (state === 1) {
        return "ON";
    }
    if (state === 0) {
        return "OFF";
    }
    return "N/A";
};
get_charge_state = function(state) {
    //console.log('ACC STATE:' + state);
    if (state === 1) {
        return "ON";
    }
    if (state === 0) {
        return "OFF";
    }
    return "N/A";
};

get_gps_state = function(state) {
    return state = 0 ? 'GPS Off' : 'GPS On';
};
get_fuel_cut_state = function(state) {
    console.log('Fuel Cut:' + state)
    if (state === 1) {
        return 'Supply BBM Off';
    }
    if (state === 0) {
        return 'Supply BBM On';
    }
    return ' Info Tidak Tersedia';
};
get_signal_gsm = function(level) {
    var str = "Tidak Ada Info Signal GSM";
    switch (level) {
        case -1:
            str = "Info Tidak Tersedia";
            break;
        case 0:
            str = "No Signal";
            break;
        case 1:
            str = "Very Weak Signal";
            break;
        case 2:
            str = "Weak Signal";
            break;
        case 3:
            str = "Good Signal";
            break;
        case 4:
            str = "Strong Signal";
            break;
        case 5:
            break;
        case 6:
            break;
    }
    return str;
};
get_alarm_name = function(level) {
    var str = "N/A";
    var alarm = parseInt(level, 10);
    switch (alarm) {
        case 1:
            str = "SOS ALARM";
            break;
        case 2:
            str = "POWER CUT ALARM";
            break;
        case 3:
            str = "LOW POWER";
            break;
        case 4:
            str = "SHOCK ALARM";
            break;
        case 5:
            str = "OVER SPEED ALARM";
            break;
        case 6:
            str = "LOW SPEED ALARM";
            break;
        case 7:
            str = "GEOFENCE IN";
            break;
        case 8:
            str = "GEOFENCE OUT";
            break;
        case 9:
            str = "OVERTIME PARK";
            break;
        case 10:
            str = "LOW SPEED ALARM";
            break;
        case 11:
            str = "LOW SPEED ALARM";
            break;
        case 12:
            str = "LOW SPEED ALARM";
            break;
    }
    return str;
};
get_signal_gps = function(level) {
    var str;
    switch (level) {
        case 0:
            break;
        case 1:
            break;
        case 2:
            break;
        case 3:
            break;
        case 4:
            break;
        case 5:
            break;
        case 6:
            break;
    }
    return str;
};
get_batt_level = function(level) {
    var str = "";
    switch (level) {
        case -1:
            str = "N/A";
            break;
        case 0:
            str = "Power Shutdown";
            break;
        case 1:
            str = "Extremly Low Batt";
            break;
        case 2:
            str = "Very Low Batt";
            break;
        case 3:
            str = "Low Batt";
            break;
        case 4:
            str = "Medium Batt";
            break;
        case 5:
            str = "High Batt";
            break;
        case 6:
            str = "Full Batt";
            break;
    }
    return str;
};